<?php
include "taobao/TopSdk.php";

class TaobaoTool
{
	// 生成淘宝对象
    static function getTopClient() {
    	$topClient = new TopClient;
    	$topClient->appkey = FrameworkModel::getInstance()->getConst("appkey");
		$topClient->secretKey = FrameworkModel::getInstance()->getConst("appsecret");
		return $topClient;
    }
    
    // 延长授权期限
	static function GetTaoBaoRefreshTokenUrl($sessionkey,$refreshToken)
	{
	    $signs = array();
	    $signs['appkey'] = FrameworkModel::getInstance()->getConst("appkey");
	    $signs['refresh_token'] = $refreshToken;
	    $signs['sessionkey'] = $sessionkey;
	    $sign = '';
	    foreach($signs as $key=>$val)
	    {
	        $sign .= $key.$val;
	    }
	    $sign .= FrameworkModel::getInstance()->getConst("appsecret");
	    $sign = md5($sign,true);
	    $len = strlen($sign);
	    $signs['sign'] = '';
	    
	    for ($i=0; $i<$len; $i++)
	    {
	        $signs['sign'] .= str_pad(dechex(ord($sign[$i])), 2, '0',STR_PAD_LEFT);
	    }
	    $signs['sign'] = strtoupper($signs['sign']);
	    
	    return FrameworkModel::getInstance()->getConst("refresh").http_build_query($signs);
	}
	
 	//验证签名函数
	static function validateSign($sign,$parameter){
	 	$checkSign = base64_encode(substr(md5($parameter,true),8,16));
	 	if($sign == $checkSign){
	 		return true;
	 	}else {
	 		return false;
	 	}	
	 }
	 
	 //JSON转array
	static function jsonToArray($json){
		$return = array();
		if(is_array($json)){
			foreach ($json as $key=>$item){
				if (is_array($item)){
					$return[$key] = $this->jsonToArray($item);
				}else{
					$return[$key] = $item;
				}
			}
		}else{
			$return[] = $json;
		}
		
		return $return;
	 }
}